<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * 
 * Model class to handle queries over db for cp users table
 * @author Babak Taraghi 'babak.taraghi@gmail.com'
 * @since September, 2011
 */
class CPUsersModel extends CI_Model {
	
	
	/**
	 * @desc String -This model language file name
	 */
	var $langFileName = "cpusers";
	
	/**
	 * @desc String - The controller name
	 * THIS IS OPTIONAL
	 * default value is 'mainController'
	 */
	//var $listControllerName = "productsController";
	
	
	
	/**
	 * @desc String - The physical table name of this model
	 */
	var $tableName = "cp_users";
	
	
	
	/**
	 * @desc Array - Columns That should be viewd in list
	 * Format : filedName=>specifications
	 * Types : link,text,boolean,time,refrenceID,categoryRefrenceID,refrence
	 * 
	 * ["type"=>"refrenceID",
	 * "modelName"=>"customersModel",
	 * "titleField"=>"fullname"]
	 *
	 */
	var $listFields = array(
		"id"=>array(
			"type"=>"link",
			"searchable"=>true,
			"orderable"=>true
		),
		"fullname"=>array(
			"type"=>"link",
			"searchable"=>true,
			"orderable"=>true
		),
		"username"=>array(
			"type"=>"text",
			"searchable"=>true,
			"orderable"=>true
		),
		"insert_time"=>array(
			"type"=>"time",
			"searchable"=>false,
			"orderable"=>true
		)
	);
	
	
	
	
	
	/**
	 * @desc Array - Form fields that should get value from user
	 * Format : filedName=>specifications
	 * Types : text,checkbox,timeSelector,categorySelect,xinha,textarea,select,timeView
	 * select : 
	 * 		"source"=>array(
	 *			"modelName"=>"typesModel",
	 *			"titleField"=>"type",
	 *			"valueField"=>"id"
	 *		)
	 *		||
	 *		"source"=>array(
	 *			"1"=>"grade1",
	 *			"2"=>"grade2",
	 *			"3"=>"grade3"
	 *		)
	 */
	var $formFields = array(
		"username"=>array(
			"type"=>"text",
			"direction"=>"ltr",
			"mandatory"=>array(
				"type"=>"text",
				"required"=>true
			)
		),
		"password"=>array(
			"type"=>"password",
			"direction"=>"ltr"
		),
		"fullname"=>array(
			"type"=>"text",
			"direction"=>"ltr",
			"mandatory"=>array(
				"type"=>"text",
				"required"=>true
			)
		),
		"email"=>array(
			"type"=>"text",
			"direction"=>"ltr",
			"mandatory"=>array(
				"type"=>"email",
				"required"=>true
			)
		),
		"enable"=>array(
			"type"=>"checkbox",
			"value"=>"1"
		),
		"last_login_time"=>array(
			"type"=>"timeView",
			"direction"=>"ltr",
			"readonly"=>"readonly"
		),
		"last_ip"=>array(
			"type"=>"text",
			"direction"=>"ltr",
			"readonly"=>"readonly"
		)
	);
	
	
	
	
	/**
	 * @desc Constructor of model
	 */
	function CPUsersModel(){
		parent::Model();
	}




	/**
	 * @desc Will return the total number of records in table
	 * @param where this is the where clause in query can be array or string
	 */
	function getTotalRecordsNumber($where=""){
		if($where != ""){
			$this->db->where($where);
		}
		$this->db->from($this->tableName);
		return $this->db->count_all_results();
	}




	/**
	 * @desc This the same as getTotalRecordsNumber but it will use 'or like' query
	 * @param where this is the where clause in query can be array or string
	 */
	function getSearchedRecordsNumber($where){
		$this->db->or_like($where);
		$this->db->from($this->tableName);
		return $this->db->count_all_results();
	}




	/**
	 * @desc This will return one record of the table
	 * @param keyFieldName this is the column name in where clause
	 * @param keyFieldValue this is the value for the specified column
	 */
	function getRecord($keyFieldName,$keyFieldValue){
		$query = $this->db->get_where($this->tableName, array($keyFieldName => $keyFieldValue));
		return $query->row();
	}




	/**
	 * @desc This is something like getRecords but it will use 'or like' query
	 * @param where this is the where clause can be string or array
	 * @param from this is starting from record number
	 * @param rows this is the finishing record number
	 * @param sortBy records will be sorted descending using this column
	 */
	function searchRecords($where,$from,$rows,$sortBy="id"){
		$this->db->order_by($sortBy,"desc");
		$this->db->or_like($where);
		if($from == 0 && $rows == 0){
			$query = $this->db->get($this->tableName);
		}else{
			$query = $this->db->get($this->tableName,$rows,$from);
		}
		return $query->result();
	}





	/**
	 * @desc This will return records of table
	 * @param where this is the where clause can be string or array
	 * @param from this is starting from record number
	 * @param rows this is the finishing record number
	 * @param sortBy records will be sorted descending using this column
	 */
	function getRecords($where="",$from=0,$rows=0,$sortBy="id"){
		$this->db->order_by($sortBy,"desc");
		if($where != ""){
			if(is_string($where))
			$this->db->where($where,NULL,FALSE);
			else
			$this->db->where($where);
		}
		if($from == 0 && $rows == 0){
			$query = $this->db->get($this->tableName);
		}else{
			$query = $this->db->get($this->tableName,$rows,$from);
		}
		return $query->result();
	}





	/**
	 * @desc This will insert a record in table
	 * @param fieldsValues array of columns and insertable values
	 */
	function insert($fieldsValues){
		$fieldsValues["insert_time"] = time();
		return $this->db->insert($this->tableName, $fieldsValues);
	}





	/**
	 * @desc This will update the fields in specified record
	 * @param id the id value of the record which should update
	 * @param fieldsValues array of columns and updatable values
	 */
	function update($id,$fieldsValues){
		return $this->db->update($this->tableName, $fieldsValues, array('id' => $id));
	}




	/**
	 * @desc This will delete records of table with the specified id
	 * @param ids array of ids which have to be delete
	 */
	function delete($ids){
		$this->db->where_in("id",$ids);
		return $this->db->delete($this->tableName);
	}
	
}
?>